﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConverterInterface;

namespace JavaConverterLibrary
{
    public class SinglelineCommentState : State
    {
        private SinglelineCommentState()  { }

        private static SinglelineCommentState instance = new SinglelineCommentState();

        public static SinglelineCommentState Instance
        {
            get {return instance;}
        }

        public override void Execute(JavaConverter owner)
        {
            if (owner.AvailableChars() <= 0)
            {
                owner.machine.Finished = true;
                owner.machine.ChangeState(NothingState.Instance);
            }

            else
            {

                if (owner.AvailableChars() >= 2)
                {
                    if (owner.Read(2) == "\r\n") // -> EOLN -> nothing
                    {
                        owner.machine.ChangeState(NothingState.Instance);
                    }
                    else
                    {
                        owner.Forward(-2);
                        owner.machine.Buffer.Append(owner.Read());
                    }
                }
                else 
                {                
                    owner.machine.Buffer.Append(owner.Read());

                }
                
            }
        }

        public override void Exit(JavaConverter owner)
        {
            owner.machine.Buffer.Replace("\"", "&quot;");
            owner.machine.Buffer.Replace("<", "&lt;");
            owner.machine.Buffer.Replace(">", "&gt;");
            owner.machine.Buffer.Insert(0, "<span class=\'comment\'>");
            owner.machine.Buffer.Append("</span>");
            owner.machine.Buffer.Append("\r\n");
            owner.FlushBuffer();
        }

        // don't do anything in enter! other states might use this
    }
}
